package com.itheima.reggie.service.impl;

import ch.qos.logback.core.db.dialect.DBUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.config.R;

import com.itheima.reggie.dao.EmployeeMapper;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.NameExistsException;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import sun.security.provider.MD5;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired(required = false)
    private EmployeeMapper employeeDao;

    /**
     * 用户登录
     *
     * @param employee
     * @return
     */
    @Override
    public R<Employee> login(Employee employee) {
//        1.根据用户名查找用户
        Employee employeebyName = employeeDao.findByName(employee);
        //        2.判断用户是否存在
        if (employeebyName == null) {
            return R.error("用户名不存在");
        }
        //        3.对比密码是否正确 用户输入的密码是明文的，需要进行md5加密才能够对比
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!(employeebyName.getPassword().equals(password))) {
            return R.error("密码错误");
        }
        //        4.查看其状态吗若是1代表可以用,
        if (employeebyName.getStatus() == 0) {
            return R.error("用户名被禁用");
        }
        //        5.返回
        return R.success(employeebyName);
    }

    /**
     * 添加用户
     *
     * @param employee
     */
    @Override
    public void addEmp(Employee employee) {
//        判断用户名是否存在
        Employee byName = employeeDao.findByName(employee);
        if (byName != null) {
            throw new NameExistsException(employee.getName());
        }
//           设置默认数据
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setStatus(1);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employeeDao.addEmp(employee);
    }

    /*
            分页查询
     */
    @Override
    public Page<Employee> empByPage(Integer page, Integer pagesize, String name) {
//       1.设置分页的开始和页面数量
       PageHelper.startPage(page,pagesize);
        List<Employee> employees = employeeDao.empByPage(name);
        PageInfo<Employee>emplist=new PageInfo<>(employees);
        Page<Employee> pagelist=new Page<>();
//        将页面数据和页面总记录数添加到page
        pagelist.setRecords(emplist.getList());
        pagelist.setTotal(emplist.getTotal());
        pagelist.setPage(emplist.getPageNum());
        pagelist.setPageSize(emplist.getPageSize());
        return pagelist;
    }

    /**
     * 修改员工信息
     * @param employee
     */
    @Override
    public void upDate(Employee employee) {
//        修改修改时间，和
        employee.setUpdateTime(LocalDateTime.now());
        employeeDao.update(employee);
    }

    @Override
    public Employee findById(Long id) {
        Employee byId = employeeDao.findById(id);
        return byId;
    }


}
